from class_segment import *

def is_inside(o,e,o1,e1) :
  if(o1 >= o and e1 <= e) :
    return True;
  else :
    return False;

def is_encaps(o,e,o1,e1) :
  if(o1 <= o and e1 >= e) :
    return True;
  else :
    return False;


def reader_seg2seg_inside(id_bloc,o,l,mysql_cursor) :
  e = o + l - 1
  req  = 'SELECT r.id_rstr, r.id_graphie, r.offset, g.graphie, g.len '
  req += 'FROM `Rstr` r, `Graphie` g '
  req += 'WHERE r.id_bloc = \'%s\' '%(id_bloc)
  req += '  AND g.id_graphie = r.id_graphie '
  req += '  AND (     (r.offset < %i AND (r.offset + g.len - 1) > %i) '%(o,o)
  req += '        OR  (r.offset > %i AND r.offset <= %i) '%(o,e)
  req += '        OR  ((r.offset + g.len - 1) > %i AND r.offset <= %i) '%(e,e)
  req += '      );'
  mysql_cursor.execute(req)


def lg2relative_id_doc(lg, mysql_cursor) :
  req  = 'SELECT d.id_document '
  req += 'FROM `Document` d '
  req += 'WHERE d.lg=\'%s\';'%(lg)

  mysql_cursor.execute(req)

  corres = {} 
  cpt = 0
  for ligne in mysql_cursor :
    cpt += 1
    corres[ligne[0]] = cpt

  return corres

def lg2relative_id_bloc(lg, mysql_cursor) :
  req  = 'SELECT b.id_bloc '
  req += 'FROM `Bloc` b,`Document` d '
  req += 'WHERE b.id_document=d.id_document'
  req += '   AND d.lg="' + lg + '";'

  mysql_cursor.execute(req)

  corres = {} 
  cpt = 0
  for ligne in mysql_cursor :
    cpt += 1
    corres[ligne[0]] = cpt

  return corres

def reader_id_document2array(id_document, mysql_cursor) :
  req  = ' SELECT b.id_bloc,b.contenu_text'
  req += ' FROM `Document` d, `Bloc` b'
  req += ' WHERE d.id_document=%i'%(id_document)
  req += '   AND d.id_document=b.id_document;';
  mysql_cursor.execute(req)

def reader_id_bloc2info_chunk(id_bloc,mysql_cursor) :
  req  = 'SELECT c.id_chunk, r.id_word, r.offset, g.len, g.graphie, c.type '
  req += 'FROM `Word` r, `Graphie` g, `Chunk` c '
  req += 'WHERE r.id_bloc=' + str(id_bloc) + ' '
  req += '  AND r.id_word = c.id_word AND g.id_graphie = r.id_graphie '
  req += 'ORDER BY c.id_chunk,r.offset;'
  mysql_cursor.execute(req)
  
  array_chunk = {}
  chunk = {}
  last_id_chunk = -1

  for ligne in mysql_cursor :
    mot = [ligne[1], ligne[2], ligne[3], ligne[4], ligne[5]]
    if last_id_chunk < 0 :
      last_id_chunk = ligne[0]
      chunk[ligne[0]] = []
    if(ligne[0] == last_id_chunk) :
      chunk[ligne[0]].append(mot)
    else :
      chunk[ligne[0]] = [mot]
    last_id_chunk = ligne[0];

  return chunk;

def lg_graphie2repartition_bloc(lg,graphie,array_corres,mysql_cursor) :
  req = '''
    SELECT r.id_graphie,r.id_bloc,COUNT(r.id_bloc)
    FROM `Rstr` r,`Bloc` b, `Document` d, `Graphie` g
    WHERE r.id_bloc = b.id_bloc
      AND b.id_document = d.id_document
      AND d.lg = '%s'
      AND r.id_graphie = g.id_graphie
      AND g.graphie = '%s'
    GROUP BY r.id_bloc,r.id_graphie
    ORDER BY r.id_graphie,r.id_bloc
  ;'''%(lg,graphie)

  mysql_cursor.execute(req)
  rep = {}
  for ligne in mysql_cursor :
    relative_id_bloc = array_corres[ligne[1]]
    rep[relative_id_bloc] = ligne[2]

  return rep


def bc2ac(v,txt) :
  ac = []
  for j,array_mot in v.iteritems() :
    am = []
    for i in array_mot :
      attr = {}
      attr['type'] = i[4]
      m = Mot(txt,i[1],i[2],attr)
      am.append(m)
    c = Chunk(am)
    ac.append(c)
  return ac

def chunk2rstr_info(ac,id_bloc,mysql_cursor) :
  res = {}
  for i,c_src in enumerate(ac) :
    o_src = c_src.get_offset()
    l_src = c_src.get_len()
    e_src = l_src + o_src
    reader_seg2seg_inside(id_bloc,o_src,l_src,mysql_cursor)
    inside = []
    union = []
    outside = []
#    print candidats
    for c in mysql_cursor :
      if (is_inside(o_src, e_src, c[2], c[2]+c[4])) :
        inside.append(c)
      elif (is_encaps(o_src, e_src, c[2], c[2]+c[4])) :
        inside.append(c)
      else :
        outside.append(c)
    res[i] = {'outside' : outside, 'inside' : inside}
  return res  

def chunk2rstr(ac,id_bloc,mysql_cursor) :
  res = {}
  for i,c_src in enumerate(ac) :
    o_src = c_src.get_offset()
    l_src = c_src.get_len()
    e_src = l_src + o_src
    reader_seg2seg_inside(id_bloc,o_src,l_src,mysql_cursor)
    res[i] = []
#    print candidats
    for c in mysql_cursor :
      res[i].append(c)
  return res  


